package com.qyhl.webtv.module_microvideo.shortvideo.theme.detail;

import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.commonlib.entity.microvideo.ShortVideoBean;
import com.qyhl.webtv.commonlib.entity.microvideo.ShortVideoThemeBean;
import com.qyhl.webtv.module_microvideo.common.VideoUrl;

import java.util.HashMap;
import java.util.List;

public class ShortVideoThemeDetailModel implements ShortVideoThemeDetailContract.ShortVideoThemeDetailModel {
    private ShortVideoThemeDetailPresenter mPresenter;

    ShortVideoThemeDetailModel(ShortVideoThemeDetailPresenter mPresenter) {
        this.mPresenter = mPresenter;
    }

    @Override
    public void getDetail(String tagId) {
        EasyHttp.get(VideoUrl.GET_SHORT_VIDEO_THEME_DETAIL)
                .params("tagId", tagId)
                .execute(new SimpleCallBack<ShortVideoThemeBean>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setDetailError("暂无任何内容，请稍后再试！");
                        } else {
                            mPresenter.setDetailError("请求失败，请重新获取！");
                        }
                    }

                    @Override
                    public void onSuccess(ShortVideoThemeBean shortVideoThemeBean) {
                        if (shortVideoThemeBean != null) {
                            mPresenter.setDetail(shortVideoThemeBean);
                        } else {
                            mPresenter.setDetailError("暂无任何内容，请稍后再试！");
                        }
                    }
                });

    }

    @Override
    public void getVideoList(String tagId, final String page,boolean isALL) {
        String url;
        HashMap<String, String> params = new HashMap<>();
        if (isALL) {
            url = VideoUrl.GET_SHORT_VIDEO_TOTAL_LIST_BY_TIME;
            params.put("actId",tagId);
        } else {
            url = VideoUrl.GET_SHORT_VIDEO_THEME_DETAIL_LIST;
            params.put("tagId",tagId);
        }
        params.put("page",page);

        EasyHttp.get(url)
               .params(params)
                .execute(new SimpleCallBack<List<ShortVideoBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        if (e.getCode() == 202) {
                            mPresenter.setListError("暂无任何内容！", !page.equals("1"));
                        } else {
                            mPresenter.setListError("请求出错，请重新获取！", !page.equals("1"));
                        }
                    }

                    @Override
                    public void onSuccess(List<ShortVideoBean> shortVideoBeans) {
                        if (shortVideoBeans != null && shortVideoBeans.size() > 0) {
                            mPresenter.setVideoList(shortVideoBeans, !page.equals("1"));
                        } else {
                            mPresenter.setListError("暂无任何内容！", !page.equals("1"));
                        }
                    }
                });
    }
}
